---
title: "Q12 Tables"
header-includes:
  - \usepackage{caption}
  - \captionsetup[table]{labelformat=empty}
geometry: margin = 0.4in
output: pdf_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(knitr)
library(survey)
library(foreign)
library(car)
library(xtable)
mil_conf.df <- read.csv("~/Dropbox/Public_Conf_Mil/Data_and_code/mil_conf.csv")
```

```{r echo = FALSE}
source("~/Dropbox/Public_Conf_Mil/Data_and_code/PCM_cleaning.R")

milconf_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

Q12_cond1 <- c("Too Partisan", "Good at Job", "Wins Wars")
Q12_cond2 <- c("Ethical", "Truthful", "Shares Values")
```

```{r echo = FALSE}
Q12_4 <- matrix(NA, nrow = 8, ncol = 12)
Q12_4[,1] <- (svyby(~Q12A_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12A_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,2] <- (svyby(~Q12A_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12A_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,3] <- (svyby(~Q12B_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12B_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,4] <- (svyby(~Q12B_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12B_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,5] <- (svyby(~Q12C_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12C_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,6] <- (svyby(~Q12C_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12C_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,7] <- (svyby(~Q12E_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12E_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,8] <- (svyby(~Q12E_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12E_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,9] <- (svyby(~Q12F_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12F_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,10] <- (svyby(~Q12F_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12F_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,11] <- (svyby(~Q12G_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12G_r2_fac, ~DOV_ASSIGNMENT_A+dem, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4[,12] <- (svyby(~Q12G_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,7] +
  svyby(~Q12G_r2_fac, ~DOV_ASSIGNMENT_A+rep, milconf_design, svymean, na.rm = TRUE)[9:16,6]) * 100
Q12_4 <- formatC(Q12_4, format = "f", digits = 0)
Q12_4 <- matrix(as.numeric(Q12_4), nrow = 8, ncol = 12)
diff2 <- Q12_4[2,] - Q12_4[1,]
diff2p <- c("+", "", "", "", "", "+", "", "+", "", "+", "", "+")
diff2 <- paste0("(", diff2p, diff2, ")", sep = "")
diff3 <- Q12_4[3,] - Q12_4[1,]
diff3p <- c("", "+", "", "", "", "", "", "", "+", "", "+", "")
diff3 <- paste0("(", diff3p, diff3, ")", sep = "")
diff4 <- Q12_4[4,] - Q12_4[1,]
diff4p <- c("", "+", "", "+", "","+", "",  "+", "", "", "+", "+")
diff4 <- paste0("(", diff4p, diff4, ")", sep = "")
diff5 <- Q12_4[5,] - Q12_4[1,]
diff5p <- c("", "+", "", "+", "", "+", "", "+", "", "", "", "")
diff5 <- paste0("(", diff5p, diff5, ")", sep = "")
diff6 <- Q12_4[6,] - Q12_4[1,]
diff6p <- c("", "", "+", "+", "+","+", "", "+", "", "+", "+", "+")
diff6 <- paste0("(", diff6p, diff6, ")", sep = "")
diff7 <- Q12_4[7,] - Q12_4[1,]
diff7p <- c("", "", "", "", "", "", "", "+", "", "+", "+", "+")
diff7 <- paste0("(", diff7p, diff7, ")", sep = "")
diff8 <- Q12_4[8,] - Q12_4[1,]
diff8p <- c("+", "", "", "", "", "", "", "", "", "", "", "+")
diff8 <- paste0("(", diff8p, diff8, ")", sep = "")
Q12_4[2,] <- paste(Q12_4[2,], diff2)
Q12_4[3,] <- paste(Q12_4[3,], diff3)
Q12_4[4,] <- paste(Q12_4[4,], diff4)
Q12_4[5,] <- paste(Q12_4[5,], diff5)
Q12_4[6,] <- paste(Q12_4[6,], diff6)
Q12_4[7,] <- paste(Q12_4[7,], diff7)
Q12_4[8,] <- paste(Q12_4[8,], diff8)
party3 <- rep(c("Dem", "Rep"), 6)
Q12_4 <- rbind(party3, Q12_4)
rownames(Q12_4) <- c("", "Control", "Republican Military", "Democrat Military", "Polarized Military",
                     "Lost Afghanistan War", "Won Afghanistan War", "Ethical Generals", "Unethical Generals")


Q12_4_tab1 <- xtable(Q12_4[,1:6])
align(Q12_4_tab1) <- "r|p{1.6cm}p{1.6cm}|p{1.6cm}p{1.6cm}|p{1.6cm}p{1.6cm}"
Q12_4_tab2 <- xtable(Q12_4[,7:12])
align(Q12_4_tab2) <- "r|p{1.6cm}p{1.6cm}|p{1.6cm}p{1.6cm}|p{1.6cm}p{1.6cm}"

addtorow3a <- list()
addtorow3a$pos <- list(0)
addtorow3a$command <- paste0(paste0('& \\multicolumn{2}{c}{', unique(Q12_cond1), '}', collapse=''), '\\\\')
addtorow3b <- list()
addtorow3b$pos <- list(0)
addtorow3b$command <- paste0(paste0('& \\multicolumn{2}{c}{', unique(Q12_cond2), '}', collapse=''), '\\\\')
```

\begin{center}
```{r echo = FALSE, results = "asis"}
print.xtable(Q12_4_tab1, add.to.row = addtorow3a, include.colnames = FALSE, hline.after = c(-1,0,1,9),
             comment = FALSE, caption.placement = "top", size = "\\large")
print.xtable(Q12_4_tab2, add.to.row = addtorow3b, include.colnames = FALSE, hline.after = c(-1,0,1,9),
             comment = FALSE, size = "\\large")
```
\end{center}